%% Three doors example
% This is an example script of the three doors game show problem. 

%% Pre cleanup
tic
clear all

%% Init

win=0;
loss=0;
loops=5000;
steps=round(loops/100);


%%
% Setup the figure to plot
figure();clf;hold on;

%% Clean Plot
axis([1 loops 0 100])
line([1 loops],[2/3*100 2/3*100],'Color','r')
line([1 loops],[1/3*100 1/3*100],'Color','b')
line([1 loops],[.5*100 .5*100],'Color','k')

%% Games
% We now loop through the games to see if switching works. 
for game=1:loops
    %%
    % Clear the doors if they exist
    clear x;

    %%
    % Setup new doors
    x(1:3)=door;
    prz=randi(3,1);
    x(prz).prize=true;

    %%
    % Pick a door at random
    cho=randi(3,1);
    x(cho).picked=true;

    %%
    % Loop through to remove the remaining door that is not picked AND has no
    % prize.
    for i=1:3
        if(x(i).prize==false && x(i).picked==false)
            x(i).removed=true;
            break
        end
    end

    %%
    % Swap the choice
    for i=1:3
        if(x(i).picked==false && x(i).removed==false)
            x(i).picked=true;
        elseif( x(i).picked==true )
            x(i).picked=false;
        end
    end

    %% 
    % Incriment if correct
    for i=1:3
        if( x(i).picked==true && x(i).prize==true)
            win=win+1;
        elseif( x(i).picked==true && x(i).prize==false)
            loss=loss+1;
        end
    end
    plot(game,win/game*100,'xr');
    plot(game,loss/game*100,'xb');
	if(mod(game,steps)==0)
		drawnow
	end
end




%% Post Script
toc